//
//  YPChoiceRedPacketViewController.m
//  ChengHuiTong
//
//  Created by ypwl on 16/8/6.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPChoiceRedPacketViewController.h"
#import "YPChoicePacketTableViewCell.h"
#import "SelectView.h"
@interface YPChoiceRedPacketViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation YPChoiceRedPacketViewController{
    
    UITableView*_tableView;
    
    NSMutableArray*_modelArr;
    
    NSMutableArray*_UnuseArr;
    
    RedbagModel*_choiceModel;
    
    
    NSIndexPath*_choiceindexPath;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    _modelArr=[[NSMutableArray alloc] init];
    
    _UnuseArr=[NSMutableArray array];
    
    for (NSDictionary*dic in self.redBagArr) {
        
        RedbagModel*model=[[RedbagModel alloc] initWithDic:dic];
        
        if (RBCP_CANUSE(self.min_tender, model.min_tender, model.max_tender)) {
            
            model.canUse=YES;
            
            [_modelArr addObject:model];
            
        }else{
            [_UnuseArr addObject:model];
            model.canUse=NO;
            
        }
        
    }
    
    [self creatTableView];

}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==0) {
        if (_modelArr.count==0) {
            
            return 0;
        }
        return _modelArr.count+1;
    }
    
    return _UnuseArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        if (indexPath.row==0) {
            
            YPChoicePacketTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell1"];
            
            if (!cell) {
                
                cell=[[YPChoicePacketTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
                
                cell.selectionStyle=0;
            }
            
            WeakSelf
            cell.UnUserredBagClick=^{
                
                
                [weakSelf UnuseRedBagButton];
            };
            
            return cell;
            
        }
        
        YPChoicePacketTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (!cell) {
            
            cell=[[YPChoicePacketTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            
            cell.selectionStyle=0;
        }
        
        if (indexPath.section==0&&indexPath.row>0) {
            
            cell.model=_modelArr[indexPath.row-1];
            
            __weak typeof (cell) weakCell=cell;
            WeakSelf
            cell.redBagClick=^(RedbagModel*model,UIButton*button){
                StrongSelf
                strongSelf->_choiceModel=model;
                
                if (strongSelf->_choiceindexPath) {
                    
                    YPChoicePacketTableViewCell*laseChoiceCell=(YPChoicePacketTableViewCell*)[strongSelf->_tableView cellForRowAtIndexPath:strongSelf->_choiceindexPath];
                    
                    if (laseChoiceCell) {
                        
                        [laseChoiceCell.backGroundButton setBackgroundImage:[UIImage imageNamed:@"可用红包-未选中"] forState:0];
                        [laseChoiceCell cellSetColorWithBool:NO];
                    }
                    
                }
                strongSelf->_choiceindexPath=indexPath;
                
                [weakCell cellSetColorWithBool:YES];
                [button setBackgroundImage:[UIImage imageNamed:@"可用红包-选中"] forState:0];
                
                
                !weakSelf.useRedBag?:weakSelf.useRedBag(model);
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
            };
            
        }
        
        
        
        if (indexPath==_choiceindexPath) {
            
            [cell.backGroundButton setBackgroundImage:[UIImage imageNamed:@"可用红包-选中"] forState:0];
            
            [cell cellSetColorWithBool:YES];
            
        }else{
            
            [cell cellSetColorWithBool:YES];
            
            [cell.backGroundButton setBackgroundImage:[UIImage imageNamed:@"可用红包-未选中"] forState:0];
        }
        
        
        return cell;
        
        
    }else{
        
        YPChoicePacketTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
        

        if (!cell) {
            
            
            cell=[[YPChoicePacketTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            
            cell.selectionStyle=0;
            
            
        }
        
        cell.model=_UnuseArr[indexPath.row];
        
        return cell;
    }
    
}
-(void)UnuseRedBagButton{
    if (self.UnuseRedBag) {
        
        self.UnuseRedBag();
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)useRedBagClick{
    
    if (!_choiceModel) {
        NSRange range = [self.title rangeOfString:@"红包"];
        NSString *str = range.location == NSNotFound?@"请选择一个加息劵":@"请选择一个红包";
        
        [MBProgressHUD showError:str toView:self.view];
        
        return;
    }
    
    if (self.useRedBag) {
        
        self.useRedBag(_choiceModel);
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==0&&indexPath.section==0) {
        
        return 60;
    }
    
    return 5+(SCREEN_WIDTH-30)*120/590.0;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row!=0) {
        
        
    }
}

-(void)creatTableView{
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    
    _tableView.dataSource=self;
    
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    _tableView.delegate=self;
    
    _tableView.tableFooterView=[[UIView alloc] init];
    
    
    if (_modelArr.count>0) {
        
        //        _tableView.tableFooterView=[self footView];
        _tableView.tableFooterView = [UIView new];
        
    }else if(_modelArr.count==0&&_UnuseArr.count==0){
        
        _tableView.tableFooterView=[SelectView footViewWithheight:_tableView.height];
        
    }else{
        
        _tableView.tableFooterView=[[UIView alloc] init];
    }
    
    [self.view addSubview:_tableView];
    
    
    
}
-(UIView*)footView{
    
    UIView*footView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 65)];
    
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    leftButton.frame=CGRectMake(30, 15, SCREEN_WIDTH-60, 35);
    
    [leftButton setTitle:@"确定使用" forState:0];
    
    [leftButton setTitleColor:[UIColor whiteColor] forState:0];
    
    [leftButton setBackgroundColor:kColor_AppNavColor];
    
    [leftButton addTarget:self action:@selector(useRedBagClick) forControlEvents:UIControlEventTouchUpInside];
    
    [footView addSubview:leftButton];
    
    return footView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
